package com.cwx.mychat.controller;

import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.ExceptionBizCodeEnum;
import com.cwx.mychat.entity.po.UserInfo;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.UserInfoService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;


@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController<UserInfoService, UserInfo>{

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 上传头像
     * @return
     */
    @PostMapping("/upload")
    public R uploadAvatar(@RequestParam("file") MultipartFile file){
        return userInfoService.upload(file);
    }

    /**
     * 编辑用户信息
     */
    @PostMapping("/edit")
    public R editUserInfo(@RequestBody UserInfo userInfo){
        userInfoService.updateById(userInfo);
        return R.ok();
    }

    /**
     * 搜索好友
     * @param searchInfo
     * @return
     */
    @GetMapping("/search")
    public R searchUserInfo(@RequestParam("searchInfo") String searchInfo){
        return userInfoService.searchFriends(searchInfo);
    }

    /**
     * 查询该ids的所有人员基础信息
     * @param ids
     * @return
     */
    @GetMapping("/selectByIds")
    public R getUserInfoByIds(@RequestParam("ids") String ids){
        return userInfoService.getUserInfoByIds(ids);
    }

    /**
     * 根据数量查询群组中的用户信息
     * @param count
     * @return
     */
    @GetMapping("/selectByCount")
    public R getUserInfoByCount(@RequestParam("count") int count, @RequestParam("ids") String ids, @RequestParam("groupId") String groupId){
        return userInfoService.getUserInfoByCount(count, ids, groupId);
    }

    @GetMapping("/selectById")
    public R getUserAllInfoById(@RequestParam("userId") String userId, @RequestParam("contactId") String contactId) throws BusinessException {
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        if(!Objects.equals(tokenUserInfoDto.getUserId(), userId)){
            throw new BusinessException(ExceptionBizCodeEnum.ACCOUNT_NOT_LOGIN);
        }
        return userInfoService.getUserAllInfoById(contactId, userId);
    }
}
